import ANNActions,ANNBody,ANNLocation,ANNBodyTimeAffect,ANNBodyActionLink
import cPickle as pickle

class ANN:
    def __init__(self,memory_file):
        self.__load__(memory_file)
        memory_file.close()
        self.__setup__()

    def __setup__(self):
        x = 0
        for a,b in enumerate(self.location):
            if self.location[b] == 0:
                x+=1

        if x == len(self.location):
            self.location[ANNLocation._places_[0]] = 1

    def __load__(self,memory):
        self.memory = pickle.load(memory)
        self.body = pickle.load(memory)
        self.location = pickle.load(memory)
        self.recent_memory = pickle.load(memory)

    def __save__(self,file_name):
        save_location = file(file_name,'w')
        pickle.dump(self.memory,save_location)
        pickle.dump(self.body,save_location)
        pickle.dump(self.location,save_location)
        pickle.dump(self.recent_memory,save_location)
        save_location.close()

    def _Punish(self,action):
        pass

    def _Reward(self,action):
        pass

    def _RecordAction(self,action):
        self.recent_memory.append(action)

    def _DoAction(self,action):
        self._RecordAction(action)
        if action in ANNActions._actions_:
            if ANNActions._actions_[action] == 'ROOMS':
                self.body[ANNActions._action_true_effect_[action]] += ANNActions._results_[action]
            else:
                self.body[ANNActions._actions_[action]] += ANNActions._results_[action]
        if action in ANNActions._secondary_:
            if ANNActions._secondary_[action] in self.memory:
                pass
            if ANNActions._secondary_[action] in self.body:
                self.body[ANNActions._secondary_[action]] += ANNActions._secondary_results_[action]

    def Update(self):
        for num, action in enumerate(self.body):
            if action in ANNBodyTimeAffect._body_:
                self.body[action] += ANNBodyTimeAffect._body_[action]

            if action in ANNBodyActionLink._trigger_:
                if self.body[action] < ANNBodyActionLink._trigger_[action]:
                    self._DoAction(ANNBodyActionLink._actionlink_[action])
                    

